package com.hancher.gamelife2.saolei;


import com.hancher.common.androidutil.LogUtil;

import java.util.Random;
import java.util.Vector;

/**
 * 作者 : Hancher ytu_shaoweijie@163.com <br/>
 * 时间 : 2023/5/20 20:11 <br/>
 * 描述 :
 */
public class BombControl {
    public static int MAX_COL = 9;//16
    public static int MAX_ROW = 9;//30
    public static int BOMB_COUNT = 10;

    public static int currentBombCount = 10;
    public static boolean isPrepare = true;

    public static SaoLeiView saoLeiView = null;
    public static BombBean[][] bombBeans = new BombBean[MAX_ROW][MAX_COL];//30 16

    public static void prepare(SaoLeiView view) {
        saoLeiView = view;
        currentBombCount = BOMB_COUNT;
        // TODO: 2023/5/20 生成雷
        bombBeans = new BombBean[MAX_ROW][MAX_COL];//30 16
        Vector<Integer> v = new Vector<>();
        Random random = new Random(System.currentTimeMillis());
        //定义一个统计变量
        int count = 0;
        while (count < BOMB_COUNT) {
            int number = random.nextInt(MAX_ROW * MAX_COL) + 1;
            //判断number是否在集合中存在
            if (!v.contains(number)) {
                //不在集合中，就添加
                v.add(number);
                count++;
            }
        }
        for (int i = 0; i < MAX_COL * MAX_ROW; i++) {//16*30
            try {
                bombBeans[i / MAX_COL][i % MAX_COL] = new BombBean(v.contains(i + 1), ShowState.HIDE_EMPTY);
            } catch (Exception e) {
                LogUtil.e(String.format("[%d,%d]", (i / MAX_COL), (i / MAX_ROW)), e);
            }
        }
        LogUtil.i("雷=[%d,%d]", bombBeans.length, bombBeans[0].length);
        LogUtil.o(bombBeans);
        isPrepare = true;
        saoLeiView.invalidate();
    }

    public static void stop() {
        saoLeiView = null;
    }

    public static void click(boolean isLongPress, int row, int col) {
        LogUtil.i("x=%d, y=%d, isLong=%b", row, col, isLongPress);
        BombBean item = bombBeans[row][col];
        if (isLongPress) {
            if (item.getShowState() == ShowState.HIDE_EMPTY) {
                item.setShowState(ShowState.HIDE_FLAG);
                currentBombCount--;
                saoLeiView.invalidate();
            } else if (item.getShowState() == ShowState.HIDE_FLAG) {
                item.setShowState(ShowState.HIDE_QUESTION);
                currentBombCount++;
                saoLeiView.invalidate();
            } else if (item.getShowState() == ShowState.HIDE_QUESTION) {
                item.setShowState(ShowState.HIDE_EMPTY);
                saoLeiView.invalidate();
            }
        } else {
            if (item.getShowState() == ShowState.HIDE_EMPTY) {
                if (item.getState()) {
                    item.setShowState(ShowState.SHOW_BOMB_FAIL);
                    TimeControl.stop();
                    saoLeiView.invalidate();
                } else {
                    item.setShowNumber(getEmptyNum(row, col));
                    item.setShowState(ShowState.SHOW_EMPTY);
                    saoLeiView.invalidate();
                    if (item.getShowNumber() == 0) {
                        // TODO: 2023/5/21 找周围8个空的作为基准
                        if (row > 0 && !bombBeans[row - 1][col].getState() && bombBeans[row - 1][col].getShowState() != ShowState.SHOW_EMPTY) {
                            click(false, row - 1, col);
                        }
                        if (row < MAX_ROW - 1 && !bombBeans[row + 1][col].getState() && bombBeans[row + 1][col].getShowState() != ShowState.SHOW_EMPTY) {
                            click(false, row + 1, col);
                        }
                        if (col > 0 && !bombBeans[row][col - 1].getState() && bombBeans[row][col - 1].getShowState() != ShowState.SHOW_EMPTY) {
                            click(false, row, col - 1);
                        }
                        if (col < MAX_COL - 1 && !bombBeans[row][col + 1].getState() && bombBeans[row][col + 1].getShowState() != ShowState.SHOW_EMPTY) {
                            click(false, row, col + 1);
                        }

                        if (row > 0 && col > 0 && !bombBeans[row - 1][col - 1].getState() && bombBeans[row - 1][col - 1].getShowState() != ShowState.SHOW_EMPTY) {
                            click(false, row - 1, col - 1);
                        }
                        if (row < MAX_ROW - 1 && col < MAX_COL - 1 && !bombBeans[row + 1][col + 1].getState() && bombBeans[row + 1][col + 1].getShowState() != ShowState.SHOW_EMPTY) {
                            click(false, row + 1, col + 1);
                        }
                        if (row > 0 && col < MAX_COL - 1 && !bombBeans[row - 1][col + 1].getState() && bombBeans[row - 1][col + 1].getShowState() != ShowState.SHOW_EMPTY) {
                            click(false, row - 1, col + 1);
                        }
                        if (row < MAX_ROW - 1 && col > 0 && !bombBeans[row + 1][col - 1].getState() && bombBeans[row + 1][col - 1].getShowState() != ShowState.SHOW_EMPTY) {
                            click(false, row + 1, col - 1);
                        }
                    }
                }
            }
        }
    }

    private static int getEmptyNum(int row, int col) {
        int lei = 0;

        if (row > 0 && col > 0 && bombBeans[row - 1][col - 1].getState()) {
            lei++;
        }
        if (row < MAX_ROW - 1 && col > 0 && bombBeans[row + 1][col - 1].getState()) {
            lei++;
        }
        if (row > 0 && col < MAX_COL - 1 && bombBeans[row - 1][col + 1].getState()) {
            lei++;
        }
        if (row < MAX_ROW - 1 && col < MAX_COL - 1 && bombBeans[row + 1][col + 1].getState()) {
            lei++;
        }

        if (row < MAX_ROW - 1 && bombBeans[row + 1][col].getState()) {
            lei++;
        }
        if (col < MAX_COL - 1 && bombBeans[row][col + 1].getState()) {
            lei++;
        }
        if (col > 0 && bombBeans[row][col - 1].getState()) {
            lei++;
        }
        if (row > 0 && bombBeans[row - 1][col].getState()) {
            lei++;
        }
        return lei;

    }

    public static boolean isSuccess() {
        int showEmptyCount = 0;
        for (int i = 0; i < MAX_ROW; i++) {
            for (int j = 0; j < MAX_COL; j++) {
                if (bombBeans[i][j].getShowState() == ShowState.SHOW_EMPTY) {
                    showEmptyCount++;
                }
            }
        }
        return MAX_COL * MAX_ROW == showEmptyCount + BOMB_COUNT;
    }

    public static class BombBean {
        boolean state;
        ShowState showState;
        int showNumber = 0;

        public BombBean(boolean state, ShowState showState) {
            this.state = state;
            this.showState = showState;
        }

        public boolean getState() {
            return state;
        }

        public BombBean setState(boolean state) {
            this.state = state;
            return this;
        }

        public ShowState getShowState() {
            return showState;
        }

        public BombBean setShowState(ShowState showState) {
            this.showState = showState;
            return this;
        }

        public int getShowNumber() {
            return showNumber;
        }

        public BombBean setShowNumber(int showNumber) {
            this.showNumber = showNumber;
            return this;
        }
    }

    enum State {
        BOMB,
        EMPTY
    }

    enum ShowState {
        HIDE_EMPTY,
        HIDE_FLAG,
        HIDE_QUESTION,

        SHOW_EMPTY,

        SHOW_BOMB_SUCCESS,
        SHOW_BOMB_FAIL,
    }
}
